//
//  CommentViewController.m
//  Socrates
//
//  Created by shy on 15/4/17.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "CommentViewController.h"

#import "CommentCell.h"
#import "RDVTabBarController.h"
#import "CommentMessageSendView.h"
#import "UserInfoViewController.h"

typedef enum {
    kLimitation_total_num_item = 0,
    kLimitation_male_num_item,
    kLimitation_female_num_item,
    kLimitation_Num,
} LimitationItems;

static const int kLimitationRowHeight = 65;

@interface CommentViewController() <CommentMsgSendViewDelegate> {
    CommentMessageSendView *msgView_;
}

@property (strong, nonatomic) UIBarButtonItem *contactsRightBarBtn;

@end

@implementation CommentViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    self.navigationController.navigationBar.hidden = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil] forCellReuseIdentifier:@"CommentCellIdentifier"];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.separatorColor = [UIColor lightGrayColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self makeMessageSendView];
    self.navigationItem.rightBarButtonItem = [self makeBarBtn];
}

- (UIBarButtonItem *)makeBarBtn {
    UIButton *settings = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 28, 25)];
    [settings setBackgroundImage:[UIImage imageNamed:@"add_friend"] forState:UIControlStateNormal];
    [settings addTarget:self action:@selector(clickRightBarButton:) forControlEvents:UIControlEventTouchUpInside];
    self.contactsRightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:settings];
    
    return self.contactsRightBarBtn;
}

- (void)clickRightBarButton:(id)sender {
    UserInfoViewController *vc = [[UserInfoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kLimitationRowHeight;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return kLimitation_Num;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CommentCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCellIdentifier"];
    UIView *selectView = [[UIView alloc] init];
    selectView.backgroundColor = [UIColor lightGrayColor];
    cell.selectedBackgroundView = selectView;
    
    cell.userName.text = @"AngelaBaby";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    
    [super viewWillDisappear:animated];
}

- (void)makeMessageSendView {
    msgView_ = [[CommentMessageSendView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds) - kToolbarHeight, CGRectGetWidth([UIScreen mainScreen].bounds), kToolbarHeight)];
    msgView_.delegate = self;
    [msgView_ setAllowAnonymous:YES];
    [self.view addSubview:msgView_];
    [self.view bringSubviewToFront:msgView_];
    
    //    if (![UserPreferences haveWritePermission]) {
    //        msgView_.userInteractionEnabled = NO;
    //        msgView_.placeHolder = NSLocalizedString(@"$$$no_story_comment_permission", );
    //    }
}

#pragma mark --
#pragma mark CommentMsgSendViewDelegate
- (void)msgChangeHeight:(CGFloat)height {
    //    if (height < 0 && currentLocation_ != nil) {
    //        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:DetailStoryCellSection_Topic];
    //        CGFloat offSet = [self tableView:tableView_ heightForRowAtIndexPath:indexPath];
    //        for (NSInteger i = 0; i <= currentLocation_.row; i++) {
    //            offSet += [self tableView:tableView_ heightForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:DetailStoryCellSection_Comment]];
    //        }
    //
    //        CGFloat originY = tableView_.contentOffset.y + CGRectGetHeight(tableView_.frame) + height;
    //        if (offSet > originY) {
    //            [tableView_ setContentOffset:CGPointMake(0, tableView_.contentOffset.y + offSet - originY) animated:NO];
    //        }
    //    }
}

- (void)onClickMessageSendButton:(BOOL)isAnony text:(NSString*)text {
    //    if (currentLocation_ == nil) {
    //        [self sendCommentToStory:isAnony comment:text];
    //    } else {
    //        [self sendCommentToComment:isAnony comment:text];
    //        currentLocation_ = nil;
    //    }
    //    [self relayoutSubviews:YES];
}

@end